const db = require('../db/index')

const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const config = require('../config')

exports.getArticleById = function(req, res) {
    const sql = 'select * from ev_article_cate where id=?'
    db.query(sql, req.params.id, (err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 1) return res.cc('获取文章分类数据失败！')
        res.send({
            status: 0,
            message: '获取文章分类数据成功！',
            data: results[0],
        })
    })
}


exports.updateArticleCateById = function(req, res) {
    console.log(req.body);
    const sql = 'select * from ev_article_cate where id<>? and (name=? or alias=?)'
    db.query(sql, [req.body.id, req.body.name, req.body.nickname], (err, results) => {
        if (err) return res.cc(err)
        if (results.length === 2)
            return res.cc('分类名称与别名被占用，请更换后重试！')
        if (results.length === 1 && results[0].name === req.body.name)
            return res.cc('分类名称被占用，请更换后重试！')
        if (results.length === 1 && results[0].alias === req.body.nickname)
            return res.cc('分类别名被占用，请更换后重试！')
        const sql1 = 'update ev_article_cate set ? where id=? '
        db.query(sql1, [req.body, req.body.id], function(err, rows) {
            if (err)
                return res.cc(err)
            if (rows.affectedRows !== 1) {
                return res.cc('更新文章分类失败！')
            } else {
                res.cc('更新文章成功', 0)
            }

        })

    })




}

exports.deleteArticleCate = function(req, res) {
    console.log(req.params.id);
    const sql = 'select * from ev_article_cate where id=? and is_delete=0'
    db.query(sql, req.params.id, function(err, rows) {
        if (err)
            return res.cc(err)
        if (rows.length !== 1) {
            console.log('id', req.user.id);
            return res.cc('分类不存在！')
        } else {
            const sql = 'update ev_article_cate set is_delete=1 where id=?'
            db.query(sql, req.params.id, function(err, rows) {
                if (err)
                    return res.cc(err)
                if (rows.affectedRows !== 1) {
                    return res.cc('删除失败')
                } else {
                    res.cc('删除成功', 0)
                }
            })
        }
    })

}



exports.addArticleCate = function(req, res) {
    console.log(req.body);
    const sql = 'insert into ev_article_cate set ?'
    db.query(sql, req.body, function(err, result) {
        if (err)
            return res.cc(err)
        if (result.affectedRows !== 1) {
            res.cc('类别添加失败')
        } else {
            res.cc('类别添加成功', 0)
        }
    })

}


exports.getArticleCate = function(req, res) {
    console.log(req.body)
    const sql = 'select * from ev_article_cate where is_delete=0'
    db.query(sql, function(err, rows) {
        if (err) {
            return res.cc(err)

        } else {
            let data = rows
            res.send({
                status: 0,
                message: 'Article cat query success',
                data,
            })
        }


    })
}